Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Executing same query gives "FailedToParse" error in nodejs

Database structure is;

{ "key" : "test" }
{ "key" : "test" }
{ "key" : "test" }
{ "key" : "test" }

In mongoshell the query,

db.collectionName.find({}, {key: 1, _id: 0})

lists the documents.

However, when I run the same query in nodejs it gives me an error as;

{
  "name": "MongoError",
  "message": "Failed to parse: comment: 1. 'key' field must be of BSON type string.",
  "ok": 0,
  "errmsg": "Failed to parse: comment: 1. 'key' field must be of BSON type string.",
  "code": 9,
  "codeName": "FailedToParse"
}

I don't understand what can be the problem?

I am aware that the question doesn't make sense, however,meaninglessly this is what I have. Is it about mongo versions or what?

edit: You are right about writing Node.js code, so here it is, the query inside an async.series call;

async.series([
  function(callback){
    db.collection("collectionName").find({}, {key: 1, _id: 0}).toArray(function(err, result){
        if(err){
            callback(err);
        } else{
            callback(null, result);
        }
    });
   }
])
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't know why you did include the mongo shell line that worked but didn't include the Node line that failed - which would be much more relevant here.

In any case, the native Mongo driver for Node (which I assume you're using here) works in a completely different way than the Mongo shell so don't expect it to work the same.

Mongo shell executes everything sequentially. In Node you have to use callbacks or promises for flow control which you don't do in the Mongo shell. Also there are some differences in how you access the collections. In the Mongo shell you usually use db.collectionName. Also in the Mongo shell you have some things predefined, like functions related to object ids for example. And finally Mongo uses BSON for data and in Node you use JavaScript object literals (more like JSON but less strict). Node uses V8 plus additional functionality and Mongo shell uses SpiderMonkey, with no functionality that is available in Node. It all have consequences in subtle differences.

You didn't include the Node line that failed so no one will be able to fix that line. But you certainly need to take a look at it and see if you're following the documentation of whetever Mongo driver or ODM you're using.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!